package com.chub.study.modules.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chub.RespResult;
import com.chub.study.util.SmartWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.chub.study.modules.service.TLogService;
import com.chub.study.modules.entity.TLog;

import java.util.Map;


/**
 * <p>
 * 日志表
 * </p>
 *
 * @author Chub
 * @since 2022-11-02
 */
@Api(tags = "日志表服务")
@Slf4j
@RestController
@RequestMapping("/modules/tlog")
public class TLogController {

    @Resource
    private TLogService tLogService;

    @ApiOperation("创建")
    @PostMapping("/create")
    public RespResult<String> create(@Valid @RequestBody TLog tLog) {
        tLogService.save(tLog);
        return RespResult.success();
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    public RespResult<String> delete(@RequestParam String id) {
        tLogService.removeById(id);
        return RespResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/modify")
    public RespResult<String> modify(@Valid @RequestBody TLog tLog) {
        tLogService.updateById(tLog);
        return RespResult.success();
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("/detail")
    public RespResult<TLog> detail(@RequestParam String id) {
        return RespResult.success(tLogService.getById(id));
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/queryByPage")
    public RespResult<IPage<TLog>> queryByPage(@RequestBody JSONObject queryObject) {
        // 测试时报文   {"sendChnlCode" : "SJYH", "current": 1, "size" : 20 }

        log.info("请求参数-->{}", JSONObject.toJSONString(queryObject));
        SmartWrapper<TLog> smartWrapper = new SmartWrapper<>();
        smartWrapper.parse(queryObject, TLog.class);

        QueryWrapper<TLog> queryWrapper = smartWrapper.getQueryWrapper();
        if (ObjectUtil.isNotEmpty(queryObject.get("sendChnlCode"))) {
            queryWrapper.eq("SEND_CHNL_CODE", queryObject.get("sendChnlCode").toString());
        }


        IPage<TLog> page = smartWrapper.generatePage();
        tLogService.page(page, queryWrapper);
        return RespResult.success(page);
    }

}